$(function(){
	
	var vm = new Vue({
		el:'#Main',
		data:{
			type:0,
//			userData:data.fans,
			userData:[],
		},
		methods:{
			//操作
			operate:function(userId){
				var that = this;
				var arr = ['添加标签','聊天','推送'];
				dmSelect(arr,function(index){
					if(index==0){
						//添加标签
						
						//获取当前数据对象
						var target;
						for(i in that.userData){
							if(that.userData[i].id == userId){
								target = that.userData[i];
							};
						};
						//传值给加标签函数
						addTag(target.tag,function(arr){
							arr.length > 0 ? target.tag = arr : target.tag = null;
						});
						
					}else if(index==1){
						//聊天
					}else{
						//推送
						location.href = 'pushMod.html?userId='+userId;
					};
				});
				
			},
		},
		created:function(){
			var that = this;
			
			//获取当前列表类型
			var userType = getUrlParam('type');
			$('.header .nav a').eq(userType).addClass('on').siblings().removeClass('on');
			that.type = userType;
			console.log(that.type)
			
			//切换数据类型
			$('.header .nav').on('click','a',function(){
				var index = $(this).index();
				location.replace('user_list.html?type='+index);
			});
			
		},
		mounted:function(){
			pullSwiper('#Main',{
				up:true,//启动下拉刷新,也可自定义函数，默认禁用
				down:true,//启用上拉加载,也可自定义函数，默认禁用
			});
			pullSwiper('#addTag');
		},
	});
	
	
	//删除标签
	$("#addTag .tag-wrap").delegate('a','click',function(){
		var target = $(this);
		dmAlert('确定要删除该标签',{
			success:function(){
				target.remove();
			},
		});
	});
	
	//新增标签
	$("#addBtn").click(function(){
		var val = $("#tag").val();
		var isAdd = true;
		$("#addTag .tag-wrap").find('.tag').each(function(){
			var tag = $(this).text();
			if(tag == val){
				isAdd = false;
			};
		});
		if(val != ''){
			if(isAdd){
				$('#addTag .tag-wrap').append(`<a><span class='tag'>${val}</span><span>×</span></a>`);
				$("#tag").val('');
			}else{
				tip('标签重复了');
			};
		};
	});
	
	//完成时间段选择
	$("#selectedBtn").click(function(){
		closeSubMain($('#selDataMask'));
	});
	
});

function refresh(pull){
	pull.innerHTML = '刷新完成';
};

function reload(pull){
	pull.innerHTML = '已加载全部数据';
};


//添加标签
function addTag(arr,callback){
	var tagHTML = '';
	
	for(i in arr){
		tagHTML += `<a><span class='tag'>${arr[i]}</span><span>×</span></a>`;
	};
	$('#addTag .tag-wrap').html(tagHTML);
	
	openSubMain('#addTagMask');
	
	//完成
	$("#achievedBtn").one('click',function(){
		var tagArr = [];
		$('#addTag .tag-wrap').find('.tag').each(function(){
			tagArr.push($(this).text());
		});
		
		if(typeof callback == 'function'){
			callback(tagArr);
		};
		closeSubMain($("#addTagMask"));
	});
	
};

//选择时间段
function selDate(obj){
	$(".date-wrap .nor").removeClass('clk');
	$(obj).next().addClass('clk');
	var val = $(obj).val();
	$(obj).next().val(val);
};


//清除时间段选择
function emptySel(){
	$(".date-wrap .nor").val('');
};
